package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.UUID;

/**
 * @author：HeXiangZhi
 * @data: 2022/12/2 22:18
 * @Description: TODO
 */
@RestController
public class ShoppingOrderController {

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    /**
     * @descriptio: TODO 创建订单
     * @params: CreateOrderRequest,HttpServletRequest
     * @return: orderId
     * @author: HeXiangZhi
     * @date: 2022/12/3 17:02
     */
    @PostMapping("/shopping/order")
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest){
        String header = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(header);
        long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);
        request.setUniqueKey(UUID.randomUUID().toString());
        String orderId = orderCoreService.createOrder(request);
        return new ResponseUtil<>().setData(orderId,"success");
    }

    /**
     * @descriptio: TODO 查询订单
     * @params:
     * @return:
     * @author: HeXiangZhi
     * @date: 2022/12/4 16:40
     */
    @GetMapping("/shopping/order")
    public ResponseData orderInfo(OrderListRequest request,HttpServletRequest servletRequest){
        String header = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(header);
        long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);
        OrderListResponse orderListResponse = orderQueryService.orderList(request);
        return new ResponseUtil<>().setData(orderListResponse,"success");
    }

    /**
     * @descriptio: TODO 查询订单详情
     * @params:
     * @return:
     * @author: HeXiangZhi
     * @date: 2022/12/4 16:49
     */
    @GetMapping("/shopping/order/{id}")
    public ResponseData orderDetailInfo(@NotNull @PathVariable("id") String orderId){
        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(orderId);
        return new ResponseUtil<>().setData(orderDetailResponse,"success");
    }




    /**
     * @Description: TODO 待付款订单取消
     * @param: 取消订单
     * @return: ResponseData
     * @Author: mxy
     * @Date: 2022/12/3 10:03
    */
    @PostMapping("/shopping/cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest){
        orderCoreService.cancelOrder(cancelOrderRequest.getOrderId());
        return new ResponseUtil().setData(null,"订单已取消");
    }


    /**
     * @Description: TODO 删除订单信息
     * @param: orderId
     * @return: ResponseData
     * @Author: mxy
     * @Date: 2022/12/3 10:34
    */
    @DeleteMapping("/shopping/order/{id}")
    public ResponseData deleteOrder(@NotNull @PathVariable("id") String orderId){
        orderCoreService.deleteOrder(orderId);
        return new ResponseUtil().setData(null,"删除成功");
    }
}
